package example.反转世界.链表反转.链表中间反转;


// https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/ 反转链表
public class Demo2 {

    // 1-2-3-4-5-null  -->   5-4-3-2-1-null

    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            //先拿到next
            ListNode next = curr.next;
            curr.next = prev;// 1-null ; 2-1-null ; 3-2-1-null ......

            prev = curr;
            curr = next;
        }
        return prev;
    }


    public static ListNode reverseList2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList2(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }


    public ListNode ReverseList(ListNode head) {
        ListNode temp = head, prev = null,current=null;
        while (temp != null) {
            ListNode next = temp.next;
            temp.next=prev;
            prev=temp;
            temp=next;
        }
        return temp;
    }

    public static void main(String[] args) {

    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}
